package org.NooLab.itexx.retrieval;

import java.util.Observable;
import java.util.Observer;

import org.NooLab.itexx.ITexxWindowCommons;
import org.NooLab.itexx.ITexxWindowIntf;
import org.NooLab.itexx.app.controller.ItexxModelController;
import org.NooLab.itexx.stuctures.RequestItem;
import org.NooLab.utilities.logging.PrintLog;





public class HttpUrlPage {

	ITexxWindowIntf app ;
	String url;
	private int webBrowserMode;
	private ITexxWindowCommons appCommons;
	private ItexxModelController itexxModelCtrl;
	
	
	PrintLog out;
	private boolean returned=false;
	
	/**
	 * 
	 * @param app   ITexxWindowIntf, reference to main app
	 * @param webBrowserMode  1=internal to itexx, 
	 * 						  2=external (hidden frame), 
	 *                             as a external process, using tcpbox for communication (for multi-threaded retrieval of large lists)
	 */
	public HttpUrlPage( ITexxWindowIntf app , int webBrowserMode){
		this.webBrowserMode = webBrowserMode;
		this.app = app ;
		
		appCommons = app.getAppCommons();
		itexxModelCtrl = appCommons.getModelController();
		
		
		out = appCommons.out ;
	}
	
	public String get(String url){
		
		String pageContent="" ;
		RequestItem requestItem;
		this.url = url;
		
		SearchRequestHandler srh = itexxModelCtrl.getSearchRequest();
		RetrievalEngine engine = new RetrievalEngine(itexxModelCtrl.getTexxWindowApp(), 1, RetrievalEngineIntf._HOST_AUTO);
		
		requestItem = RequestItem.prepareRequest( url );
		
		returned = false;
		new PageServer(requestItem);
		
		
		while (returned == false){
			out.delay(10);
		}
		return pageContent;
		
	}
	
	
	class PageServer implements Observer, Runnable{
		
		RequestItem requestItem ;
		boolean isRunning = false;
		Thread pgsThrd ;
		
		// ------------------------------------------------
		public PageServer(RequestItem rqItem){
			requestItem = rqItem ;
			pgsThrd = new Thread(this,"pgsThrd");
			pgsThrd.start();
		}
		// ------------------------------------------------

		@Override
		public void run() {
			
			// srh.updateRequestEntries( engine, requestItem) ;
			
			// check DjWebBrowserHost for completed message
			
			// get content from WebBrowser
			
		}

		@Override
		public void update(Observable arg0, Object arg1) {
			
			
		}
		
	}
	
}
